package com.jiuyin.dianjing.ui.activity.setting;

import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.TabAdapter;
import com.jiuyin.dianjing.event.HomepageMsgEvent;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.homepage.HomepageFirst;
import com.jiuyin.dianjing.ui.fragment.homepage.HomepageSecond;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_USERID;

/**
 * 我的主页,需要动态更新数据
 */
public class SettingMyHomePageActivity extends BaseActivity {
    protected final List<Pair<String, Fragment>> mFragments = new ArrayList<>();
    @BindView(R.id.vp_view_container)
    ViewPager mViewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_id)
    TextView mTvID;
    @BindView(R.id.tv_sign)
    TextView mTvSign;
    @BindView(R.id.tv_attention)
    TextView mTvAttention;
    @BindView(R.id.tv_subscription)
    TextView mTvSubscription;
    @BindView(R.id.tv_fans)
    TextView mTvFans;
    private TabAdapter mTabAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_setting_homepage;
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String userId = intent.getStringExtra(KEY_USERID);
        mFragments.add(Pair.create(getString(R.string.home_page_text_nav_first), HomepageFirst.newInstance(userId)));
        mFragments.add(Pair.create(getString(R.string.home_page_text_nav_second), HomepageSecond.newInstance(userId)));
        initTabLayout();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void initView() {
        setTitle(R.string.my_home_page);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(HomepageMsgEvent event) {
        refresh(event);
    }

    protected void initTabLayout() {
        mTabAdapter = new TabAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mTabAdapter);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        mTabLayout.setupWithViewPager(mViewPager);
    }

    /**
     * 更新数据
     *
     * @param event
     */
    private void refresh(HomepageMsgEvent event) {
        mTvAttention.setText(getString(R.string.attention, event.attention_num));
        mTvFans.setText(getString(R.string.fans, event.fans_num));
        mTvSubscription.setText(getString(R.string.subscribe, event.topic_num));
        mTvName.setText(event.nickname);
        mTvID.setText(getString(R.string.id, event.id));
        mTvSign.setText(event.sign);
    }
}
